<template>
  <div class="shop_manage">
    <p>基本信息</p>
    <el-form :model="formData" :rules="rules" ref="ruleForm">
      <el-form-item label="姓名:" prop="username">
        <el-input v-model="formData.username" size="mini"></el-input>
      </el-form-item>

      <el-form-item label="电话:" prop="telephone">
        <el-input v-model="formData.telephone" size="mini"></el-input>
      </el-form-item>
    </el-form>

    <p>店铺列表</p>
    <el-table max-height="340px" :data="formData.shopList" border>
      <el-table-column label="店铺照片" align="center">
        <template slot-scope="scope">
          <img class="shop-logo" :src="scope.row.logo">
        </template>
      </el-table-column>
      <el-table-column label="店铺名称" prop="shopName" align="center"></el-table-column>
      <el-table-column label="经营类型" prop="" align="center">
        <template slot-scope="scope">
          {{ scope.row.businessType | typeFilter}}
        </template>
      </el-table-column>
      <el-table-column label="管理员数量(个)" prop="managerList.length" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="managerShow = true,managerRow = scope.row">管理员</el-button>
        </template>
      </el-table-column>
    </el-table>

    <p style="text-align: center;margin-top: 20px;">
      <el-button size="mini" type="primary" @click="submit('ruleForm')">保存</el-button>
      <el-button size="mini" @click="$emit('closeDialog')">关闭</el-button>
    </p>

    <el-dialog :visible.sync="managerShow" :append-to-body="true" width="520px" :close-on-click-modal="false" :close-on-press-escape="false">
      <manager v-if="managerShow" :row="managerRow" @closeDialog="managerShow = false" @refresh=""/>
    </el-dialog>


  </div>
</template>

<script>
  import manager from './manager.vue'

  export default {
    components: { manager },
    props: ['row'],
    filters: {
      typeFilter(type) {
        switch(type) {
          case 1:
            return '餐饮';
          case 2:
            return '加油站';
          case 3:
            return '洗车';
          case 4:
            return '养车';
          case 5:
            return '商超';
          case 6:
            return '其他'
        }
      }
    },
    data() {
      var validatorPhone = function (rule, value, callback) {
        if (value == undefined) {
          callback(new Error('手机号不能为空'))
        } else if (!/^1\d{10}$/.test(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        formData: {},
        managerShow: false,
        rules: {
          username: [{required: true, message: '请输入姓名', trigger: 'blur' }],
          telephone: [{ required: true, validator: validatorPhone, trigger: 'blur' }]
        }
      }
    },

    mounted() {
      this.formData = this.row;
    },

    methods: {
      submit(formName) {
        this.$refs[formName].validate((valid) => {
          if(valid) {
            this.formData.managerId = this.formData.id;
            this.$axios({
              method: 'post',
              url: '/shop/shop-manager/update-manager',
              data: this.formData
            }).then((res) => {
              if(res.code == 200) {
                this.$message.success('修改信息成功');
                this.$emit('closeDialog');
              } else {
                this.$message.warning(res.desc)
              }
            })
          } else {
            return false
          }
        })
      }
    }
  }
</script>

<style scoped>
  .shop_manage {
    padding: 0 20px;
  }

  p {
    font-size: 20px;
    font-weight: bold;
    margin-top: 0;
  }

  .el-input {
    width: 200px;
  }

  .shop-logo {
    height: 70px;
  }





</style>
